package com.group4.order.controller;


import com.group4.order.entity.Order;
import com.group4.order.service.OrderService;
import com.study.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;


/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @RequestMapping("/add")
    public ResponseResult addOrder(@RequestBody Order order) throws Exception {
        final Object orderNumber = orderService.addOrder(order);
        return new ResponseResult(200,"添加订单成功",orderNumber);
    }

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @RequestMapping("/find/{id}")
    public ResponseResult<Order> findOrderById(@PathVariable String id){
         return new ResponseResult<>(orderService.findOrderById(id));
    }

    /**
     * 判断订单是否处于已支付状态
     * @param id
     * @return
     */
    @RequestMapping("/pay/{id}")
    public Integer findPayById(@PathVariable String id){
        final Integer byIdPay = orderService.findByIdPay(id);
        return byIdPay;
    }

    /**
     *根据id删除订单
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public ResponseResult deleteOrderById(@PathVariable String id){
      orderService.deleteById(id);
      return new ResponseResult(200,"删除成功");
    }

    @RequestMapping("/getAll")
    public ResponseResult deleteOrderById(){
        return new ResponseResult(orderService.getAll());
    }

    /**
     * 根据订单id修改订单状态
     * @param
     * @return
     */
    @GetMapping("/payOrder/{id}")
    public ResponseResult payOrder(@PathVariable("id") String id){
        //根据订单编号修改订单状态,将订单状态设置为1
        final int i = orderService.updateStatusByNum(id);
        if(i!=0) {
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    @GetMapping("/getByOrderNo/{orderNumber}")
    public ResponseResult<Order> getByOrderNumber(@PathVariable("orderNumber") String orderNo){
        final Order order = orderService.getByOrderNo(orderNo);
        if(order==null){
            return new ResponseResult<>(400,"",null);
        }
        return new ResponseResult<>(order);

    }


}

